
//3427.变长子数组求和
class Solution {
public:
    int subarraySum(vector<int>& nums) {
        int n = nums.size(),ret = 0;
        vector<int> lsum(n+1);

        for(int i = 0; i < n;i++)
        {
            lsum[i+1] = lsum[i] + nums[i];      //计算前缀和
            int start = max(0,i - nums[i]);     //查找起始位置
            ret += lsum[i+1] - lsum[start];     //添加答案
        }    
        
        return ret;
    }
};